'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var buildService = require('../../../../services/buildService');
var dataApi = require('../../../../libs/dataApi.js');
var actCarbonData = require('../../../../libs/war/actCarbonData');
var atkData = require('../../../../libs/war/atkData');
var actCarbonConf = require('../../../../config/actCarbonConf');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var carbon_id = msg.carbon_id;

    if (!carbon_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var model_name;
    var carbon_conf = actCarbonConf.getCarbonConfById(carbon_id);
    if (carbon_conf.type === 1) {
        model_name = 'gold_carbon';
    } else {
        model_name = 'food_carbon';
    }

    var player_manager = new PlayerManager(role_id);
    var record_id;
    var war_time = constantUtil.value('war_time');
    var atk_data;
    var def_data;
    var act_carbon_model;

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    [model_name, 'role', 'copy', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                act_carbon_model = player_manager[model_name];
                var role_model = player_manager.role;
                var copy_model = player_manager.copy;

                // 开放等级判断
                var active_carbon_open_lvl = constantUtil.value('active_carbon_open_lvl');
                if (role_model.lv < active_carbon_open_lvl) {
                    return next(null, {
                        code: Code.ACTIVE_CARBON_NOT_OPEN
                    });
                }

                // 普通副本通关判断
                if (carbon_conf.need_carbon > 0) {
                    var copy = copy_model.getCopy(carbon_conf.need_carbon);
                    if (!copy || copy.star < 1) {
                        return next(null, {
                            code: Code.ACTIVE_CARBON_COPY_NOT_PASSED
                        });
                    }
                }

                // 前置副本未通关
                if (carbon_conf.need_id > 0 && act_carbon_model.carbon_passed < carbon_conf.need_id) {
                    return next(null, {
                        code: Code.ACTIVE_CARBON_PRE_NOT_PASSED
                    });
                }

                // 扣除次数，判断次数是否效
                if (!act_carbon_model.decFightTimes()) {
                    return next(null, {
                        code: Code.ACTIVE_CARBON_TIME_LIMIT
                    });
                }

                // 冷却CD判断
                var now_tm = moment().unix();
                if (act_carbon_model.freeze_tm > now_tm) {
                    return next(null, {
                        code: Code.ACTIVE_CARBON_IN_CD
                    });
                }
                var active_carbon_cd_time = constantUtil.value('active_carbon_cd_time');
                act_carbon_model.freeze_tm = now_tm + active_carbon_cd_time;

                // 敌方数据
                def_data = _getDef(carbon_id);
                if (!def_data) {
                    return next(null, {
                        code: Code.COPY_NOT_EXIST
                    });
                }

                // 副本次数统计
                taskService.addGoldFoodCopyNum(player_manager);

                // 我方数据
                _getAtk(role_id, cb);
            },
            function (info, cb) {
                atk_data = info;

                record_id = carbon_id;
                act_carbon_model.battle_tm = moment().unix();

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    record_id: record_id,
                    war_time: war_time,
                    carbon_id: carbon_id,
                    atk: atk_data,
                    def: def_data
                }
            });
        }
    );
};

/**
 * 获取攻击方数据
 * @return {[type]} [description]
 */
var _getAtk = function (role_id, cb) {
    atkData.getData(role_id, cb);
};

/**
 * 获取防守方数据
 * @return {[type]} [description]
 */
var _getDef = function (copy_id) {
    return actCarbonData.getData(copy_id);
};
